package com.base.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.system.exception.HttpException;
import com.base.system.jpa.entity.JpaPermission;
import com.base.system.jpa.entity.JpaRole;
import com.base.system.jpa.repository.PermissionRepository;
import com.base.system.jpa.repository.RoleRepository;
import com.base.system.mybatisPlus.entity.MybatisRole;
import com.base.system.mybatisPlus.entity.MybatisRolePermission;
import com.base.system.mybatisPlus.mapper.RoleMapper;
import com.base.system.mybatisPlus.mapper.RolePermissionMapper;
import com.base.system.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleRepository roleRepository;
    @Autowired
    private PermissionRepository permissionRepository;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;


    @Override
    public IPage<MybatisRole> page(Page<MybatisRole> page, MybatisRole role) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like(StrUtil.isNotEmpty(role.getNameZh()), "name_zh", role.getNameZh());
        queryWrapper.like(StrUtil.isNotEmpty(role.getRoleKey()), "role_key", role.getRoleKey());
        queryWrapper.orderByDesc("create_time");
        return roleMapper.selectPage(page, queryWrapper);
    }

    @Override
    public void save(MybatisRole role) {
        valid(role, true);
        roleMapper.insert(role);
    }

    private void valid(MybatisRole role, boolean isAdd) {
        Optional<JpaRole> firstByNameZh = roleRepository.findFirstByNameZh(role.getNameZh());
        if (firstByNameZh.isPresent() && (!firstByNameZh.get().getId().equals(role.getId()) || isAdd))
            throw new HttpException("角色名称重复");
        Optional<JpaRole> firstByRoleKey = roleRepository.findFirstByRoleKey(role.getRoleKey());
        if (firstByRoleKey.isPresent() && (!firstByRoleKey.get().getId().equals(role.getId()) || isAdd))
            throw new HttpException("角色key值重复");
    }

    @Override
    public void update(MybatisRole role) {
        valid(role, false);
        roleMapper.updateById(role);
    }

    @Override
    public void delete(Long id) {
        roleMapper.deleteById(id);
    }

    @Override
    public void addPer(Long roleId, List<Long> perIds) {
        JpaRole jpaRole = roleRepository.findById(roleId).get();
        List<JpaPermission> permissions = permissionRepository.findAllByIdIn(perIds);
        jpaRole.setPermissions(permissions);
        roleRepository.save(jpaRole);
    }

    @Override
    public List<Long> currentRoleWithPres(Long roleId) {
        QueryWrapper<MybatisRolePermission> wrapper = new QueryWrapper();
        wrapper.eq("role_id", roleId);
        List<MybatisRolePermission> rolePermissions = rolePermissionMapper.selectList(wrapper);
        if (CollUtil.isNotEmpty(rolePermissions))
            return rolePermissions.stream().map(t -> t.getPerId()).collect(Collectors.toList());
        return null;
    }

    @Override
    public List<MybatisRole> all() {
        return roleMapper.selectList(new QueryWrapper<>());
    }
}
